﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Contacts;
using System.Text.RegularExpressions;
#if!__MonoCS__
//using Microsoft.Office.Interop.Outlook;
#endif
using System.Configuration;
using System.Security.Principal;

namespace Workbench
{
	public partial class Bench : Form
	{
		
		//private Form cont = new ContactsAddEdit();
		private DataSet contacts;
		BindingNavigator contactsbn = new BindingNavigator(true);
		private BindingSource contactsbs = new BindingSource();
		protected ProjectTypes projects;
		private Contacts.JobFilter Jobs;
		
		#region CTOR
		/// <summary>
		/// Initializes a new instance of the <see cref="Contacts.Workbench"/> class.
		/// </summary>
		public Bench ()
		{
			InitializeComponent ();
			prog_bar.Visible = false;
			projects = ProjectTypes.None;
			if (Workbench.Properties.Settings.Default.CURRENTJOBS) { 
			  Jobs = JobFilter.Current;
			} else {
			  Jobs = JobFilter.All;
			}
			buttonManpower.Visible = Environment.UserName == "bcrawford" ? true : false;
		}
		
		#endregion
		
		/// <summary>
		/// this event procedure opens the store contacts form
		/// </summary>
		/// <param name='sender'>
		/// Sender.
		/// </param>
		/// <param name='e'>
		/// E.
		/// </param>
		private void but_strcontacts_Click(object sender, EventArgs e)
		{
			bool strcontfo = CheckForm("Contacts");

			try
			{
				if (strcontfo == false)
				{
					Thread t = new Thread(new ParameterizedThreadStart(startcontacts));
					t.SetApartmentState(ApartmentState.STA);
					t.Start(new MainContactData (projects, Jobs, "trident"){});
				}
				else
				{
					MessageBox.Show("A Project Contacts application is already running!", "Application Already Running", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					return;
				}
			}
			catch (System.Exception ex)
			{
				string subject = "Workbench store contacts click 3rd floor Error";
				ErrorHandling.ErrorEmail(subject, ex);
			}
		}

		//this buttons opens the contacts form
		private void but_contacts_Click(object sender, EventArgs e)
		{
			bool contfo = CheckForm("ContactsAddEdit");

			try
			{
				if (contfo == false)
				{
					MySqlConn conn = new MySqlConn();
					conn.Connect("contacts");
					conn.LoadAllContacts();
					conn.Disconnect();
					contacts = conn.dscontacts;
					contactsbs.DataSource = contacts;
					contactsbn.BindingSource = contactsbs;
					Thread t2 = new Thread(new ThreadStart(opencontform));
					t2.SetApartmentState(ApartmentState.STA);
					t2.Start();
				}
				else
				{
					MessageBox.Show("A Contacts application is already running!", "Application Already Running", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					return;
				}
			}
			catch (System.Exception ex)
			{
				string subject = "Workbench contacts click Error";
				ErrorHandling.ErrorEmail(subject, ex);
			}
		}

		//this button sets all the outlook views in the public folder 0001 Store Contacts
		private void but_ol_Click(object sender, EventArgs e)
		{
			func.FolderView(prog_bar);
		}

		private void but_trid_Click(object sender, EventArgs e)
		{
			MessageBox.Show("This is currently under construction.", "Under Construction", MessageBoxButtons.OK);
		}

		private void but_strcontacts_remodel_Click (object sender, System.EventArgs e)
		{
			
			bool strcontfo = CheckForm ("Contacts2");

			try {
				if (strcontfo == false) {
					Thread t = new Thread (new ParameterizedThreadStart (startcontacts));
					t.SetApartmentState (ApartmentState.STA);
					t.Start (new MainContactData (projects, Jobs, "spock") {});
				} else {
					MessageBox.Show (
						"A Project Contacts application is already running!",
						"Application Already Running",
						MessageBoxButtons.OK,
						MessageBoxIcon.Stop
					);
					return;
				}
			} catch (System.Exception ex) {
				string subject = "Workbench store contacts click 2nd floor Error";
				ErrorHandling.ErrorEmail (subject, ex);
			}
		}

		//this starts a new instance of the store contacts form
		public void startcontacts (object obj)
		{
			try {
				var ContactData = obj as MainContactData;
				System.Windows.Forms.Application.EnableVisualStyles ();
				System.Windows.Forms.Application.SetCompatibleTextRenderingDefault (false);
				System.Windows.Forms.Application.Run (new Contacts.Contacts (ContactData));
				
			}
			catch (System.Exception ex)
			{
				string subject = "Workbench contacts click Error";
				ErrorHandling.ErrorEmail(subject, ex);
			}
		}

		//this starts a new instance of the contacts form
		private void opencontform ()
		{
			//DataGridView grd_jr_cont;
			System.Windows.Forms.Application.EnableVisualStyles ();
			System.Windows.Forms.Application.SetCompatibleTextRenderingDefault (false);
			System.Windows.Forms.Application.Run (new ContactsAddEdit (contacts, contactsbs, "contacts", null));
		}

		//this prevents opening multiple of store contacts or contacts forms
		private bool CheckForm (string ApplicationName)
		{
			int count =  System.Windows.Forms.Application.OpenForms.Count;
			bool result = false;
			for (int i = 0; i <= count; i++) {
				Form f = System.Windows.Forms.Application.OpenForms [i];
				string[] name = f.Name.Split (new Char[] { '.' });
				
				if (name [0] == ApplicationName) {
					result = true;
				} else {
					break;
				}
			}
			return result;
		}
		
		void RemoteLoad (object sender, EventArgs e)
		{
			if (Workbench.Properties.Settings.Default.EXPANSION)
				projects |= ProjectTypes.Expansion;
			if (Workbench.Properties.Settings.Default.GROUNDUP)
				projects |= ProjectTypes.GroundUp;
			if (Workbench.Properties.Settings.Default.REMODEL)
				projects |= ProjectTypes.Remodel;
			if (Workbench.Properties.Settings.Default.TAKEOVER)
				projects |= ProjectTypes.TakeOver;
			if (Workbench.Properties.Settings.Default.REMODELC)
				projects |= ProjectTypes.RemodelC;
			if (Workbench.Properties.Settings.Default.FIXTURE)
				projects |= ProjectTypes.Fixturing;
			if (Workbench.Properties.Settings.Default.TENANT)
				projects |= ProjectTypes.Tenant;
			if (Workbench.Properties.Settings.Default.SPECIAL)
				projects |= ProjectTypes.Special;
			if (Workbench.Properties.Settings.Default.OSR)
				projects |= ProjectTypes.OnSiteRelo;
			if (Workbench.Properties.Settings.Default.RELO)
				projects |= ProjectTypes.Relo;
			if (Workbench.Properties.Settings.Default.LANDBANK)
				projects |= ProjectTypes.LandBank;
		}
		
		void SelectJobTypesToolStripMenuItemClick (object sender, EventArgs e)
		{
			WorkbenchOptions o = new WorkbenchOptions ();
			o.StartPosition = FormStartPosition.CenterParent;
			o.FormClosed += new FormClosedEventHandler (WorkbenchOptionsClosed);
			o.ProjectTypeChanged += new WorkbenchOptions.ProjectTypeChangedHandler (ProjectTypeChanger);
			o.ShowDialog ();
		}
		
		void WorkbenchOptionsClosed (object sender, EventArgs e)
		{
			WorkbenchOptions o = sender as WorkbenchOptions;
			o.ProjectTypeChanged -= new WorkbenchOptions.ProjectTypeChangedHandler (ProjectTypeChanger);
			Jobs = o.GetJobsFilter;
		}
		
		protected void ProjectTypeChanger (ProjectTypes ProjectType)
		{
		  //use bitwise XOR to add and remove project types from the enumerator
			projects = projects ^ ProjectType; 
		}

		/// <summary>Emmbedded class to hold parameters for starting the threads</summary>
		public class MainContactData
		{
			/// <summary>
			/// Initializes a new instance of the <see cref="Contacts.Workbench+MainContactData"/> class.
			/// </summary>
			/// <param name='ProjectType'>
			/// Project type.
			/// </param>
			/// <param name='DatatoLoad'>
			/// Datato load.
			/// </param>
			public MainContactData(ProjectTypes projectType, JobFilter job, string DatatoLoad)
			{
				ProjectType = projectType;
				JobToLoad = job;
				DataToLoad = DatatoLoad;
			}
			/// <summary>
			/// Gets the type of the project.
			/// </summary>
			/// <value>
			/// The type of the project.
			/// </value>
			public ProjectTypes ProjectType { get; private set;}
			/// <summary>
			/// Gets the data to load.
			/// </summary>
			/// <value>
			/// The data to load.
			/// </value>
			public string DataToLoad { get; private set;}
			public JobFilter JobToLoad { get; private set;}
		}
		
	}
}
